# vim: set ft=ruby:
# frozen_string_literal: true

def find_ansible_cfg
  path = Pathname.new(Dir.pwd)
  until path.root?
    ansible_cfg = path + 'ansible.cfg'
    return path if ansible_cfg.exist?
    path = path.parent
  end
  nil # return nil if not found
end

Vagrant.configure('2') do |config|
  config.vm.define 'crowdsec'

  if ARGV.any? { |arg| arg == 'up' || arg == 'provision' } && !ARGV.include?('--no-provision')
    unless ENV['DB_BACKEND']
      $stderr.puts "\e[31mThe DB_BACKEND environment variable is not defined. Please set up the environment and try again.\e[0m"
      exit 1
    end
  end

  config.vm.provision 'shell', path: 'bootstrap' if File.exist?('bootstrap')
  config.vm.synced_folder '.', '/vagrant', disabled: true

  config.vm.provider :libvirt do |libvirt|
    libvirt.cpus = 1
    libvirt.memory = 1536
  end

  path = find_ansible_cfg
  if !path
    puts "ansible.cfg not found"
  end

  config.vm.provision 'ansible' do |ansible|
    # ansible.verbose = 'vvvv'
    ansible.config_file = (path + 'ansible.cfg').to_s
    ansible.playbook = (path + 'run_all.yml').to_s
    ansible.compatibility_mode = "2.0"
  end

  # same as above, to run the steps separately

  # config.vm.provision 'ansible' do |ansible|
  #   ansible.config_file = (path + 'ansible.cfg').to_s
  #   ansible.playbook = (path + 'provision_dependencies.yml').to_s
  #   ansible.compatibility_mode = "2.0"
  # end

  # config.vm.provision 'ansible' do |ansible|
  #   ansible.config_file = (path + 'ansible.cfg').to_s
  #   ansible.playbook = (path + 'provision_test_suite.yml').to_s
  #   ansible.compatibility_mode = "2.0"
  # end

  # config.vm.provision 'ansible' do |ansible|
  #   ansible.config_file = (path + 'ansible.cfg').to_s
  #   ansible.playbook = (path + 'install_binary_package.yml').to_s
  #   ansible.compatibility_mode = "2.0"
  # end

  # config.vm.provision 'ansible' do |ansible|
  #   ansible.config_file = (path + 'ansible.cfg').to_s
  #   ansible.playbook = (path + 'prepare_tests.yml').to_s
  #   ansible.compatibility_mode = "2.0"
  # end

  # config.vm.provision 'ansible' do |ansible|
  #   ansible.config_file = (path + 'ansible.cfg').to_s
  #   ansible.playbook = (path + 'run_tests.yml').to_s
  #   ansible.compatibility_mode = "2.0"
  # end
end
